<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="shopId">
      <el-input v-model="dataForm.shopId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="家长电话" prop="parentPhone">
      <el-input v-model="dataForm.parentPhone" placeholder="家长电话"></el-input>
    </el-form-item>
    <el-form-item label="推荐人" prop="refereeName">
      <el-input v-model="dataForm.refereeName" placeholder="推荐人"></el-input>
    </el-form-item>
    <el-form-item label="学生妈" prop="studentMather">
      <el-input v-model="dataForm.studentMather" placeholder="学生妈"></el-input>
    </el-form-item>
    <el-form-item label="学生妈电话" prop="matherPhone">
      <el-input v-model="dataForm.matherPhone" placeholder="学生妈电话"></el-input>
    </el-form-item>
    <el-form-item label="学生爸" prop="studentFather">
      <el-input v-model="dataForm.studentFather" placeholder="学生爸"></el-input>
    </el-form-item>
    <el-form-item label="学生爸电话" prop="fatherPhone">
      <el-input v-model="dataForm.fatherPhone" placeholder="学生爸电话"></el-input>
    </el-form-item>
    <el-form-item label="其它联系人" prop="other">
      <el-input v-model="dataForm.other" placeholder="其它联系人"></el-input>
    </el-form-item>
    <el-form-item label="其它联系人电话" prop="otherPhone">
      <el-input v-model="dataForm.otherPhone" placeholder="其它联系人电话"></el-input>
    </el-form-item>
    <el-form-item label="家长角色0妈1爸2姑3姑父4姨5姨夫6婶7叔8奶9爷" prop="parentRole">
      <el-input v-model="dataForm.parentRole" placeholder="家长角色0妈1爸2姑3姑父4姨5姨夫6婶7叔8奶9爷"></el-input>
    </el-form-item>
    <el-form-item label="剩余积分" prop="residualintegral">
      <el-input v-model="dataForm.residualintegral" placeholder="剩余积分"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="新增时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="新增时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          shopId: '',
          parentPhone: '',
          refereeName: '',
          studentMather: '',
          matherPhone: '',
          studentFather: '',
          fatherPhone: '',
          other: '',
          otherPhone: '',
          parentRole: '',
          residualintegral: '',
          remark: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          shopId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          parentPhone: [
            { required: true, message: '家长电话不能为空', trigger: 'blur' }
          ],
          refereeName: [
            { required: true, message: '推荐人不能为空', trigger: 'blur' }
          ],
          studentMather: [
            { required: true, message: '学生妈不能为空', trigger: 'blur' }
          ],
          matherPhone: [
            { required: true, message: '学生妈电话不能为空', trigger: 'blur' }
          ],
          studentFather: [
            { required: true, message: '学生爸不能为空', trigger: 'blur' }
          ],
          fatherPhone: [
            { required: true, message: '学生爸电话不能为空', trigger: 'blur' }
          ],
          other: [
            { required: true, message: '其它联系人不能为空', trigger: 'blur' }
          ],
          otherPhone: [
            { required: true, message: '其它联系人电话不能为空', trigger: 'blur' }
          ],
          parentRole: [
            { required: true, message: '家长角色0妈1爸2姑3姑父4姨5姨夫6婶7叔8奶9爷不能为空', trigger: 'blur' }
          ],
          residualintegral: [
            { required: true, message: '剩余积分不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '新增时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/parent/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shopId = data.parent.shopId
                this.dataForm.parentPhone = data.parent.parentPhone
                this.dataForm.refereeName = data.parent.refereeName
                this.dataForm.studentMather = data.parent.studentMather
                this.dataForm.matherPhone = data.parent.matherPhone
                this.dataForm.studentFather = data.parent.studentFather
                this.dataForm.fatherPhone = data.parent.fatherPhone
                this.dataForm.other = data.parent.other
                this.dataForm.otherPhone = data.parent.otherPhone
                this.dataForm.parentRole = data.parent.parentRole
                this.dataForm.residualintegral = data.parent.residualintegral
                this.dataForm.remark = data.parent.remark
                this.dataForm.createTime = data.parent.createTime
                this.dataForm.updateTime = data.parent.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/parent/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'shopId': this.dataForm.shopId,
                'parentPhone': this.dataForm.parentPhone,
                'refereeName': this.dataForm.refereeName,
                'studentMather': this.dataForm.studentMather,
                'matherPhone': this.dataForm.matherPhone,
                'studentFather': this.dataForm.studentFather,
                'fatherPhone': this.dataForm.fatherPhone,
                'other': this.dataForm.other,
                'otherPhone': this.dataForm.otherPhone,
                'parentRole': this.dataForm.parentRole,
                'residualintegral': this.dataForm.residualintegral,
                'remark': this.dataForm.remark,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
